package com.traffic.between.service;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.traffic.between.config.BetweenProperties;


public class YunzxServiceImpl {
	private static Logger logger = Logger.getLogger(YunzxServiceImpl.class);
	public static YunzxServiceImpl service = new YunzxServiceImpl();
	
	private static String HOST_ORDER_PATH = BetweenProperties.YUNZX_ORDER_URL;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.YUNZX_ORDERSTATUS_URL;
	private static String YUNZX_ACCOUNT = BetweenProperties.YUNZX_ACCOUNT;
	private static String YUNZX_KEY = BetweenProperties.YUNZX_KEY;
	
	
	
	public String order(String pkgNo, String phoneNo, String tradeNo,String region) {
	
        String result = "";
       
	
		try {
			String waitSign = "account="+YUNZX_ACCOUNT+"&mobile="+phoneNo+"&package="+pkgNo+"&key="+YUNZX_KEY;
			String sign = getMD5(waitSign);
	        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
	        pairList.add(new BasicNameValuePair("Action","flowRecharge"));
	        pairList.add(new BasicNameValuePair("V","1.1"));
	        pairList.add(new BasicNameValuePair("Range",region));
	        pairList.add(new BasicNameValuePair("OutTradeNo",tradeNo));
	        pairList.add(new BasicNameValuePair("Account",YUNZX_ACCOUNT));
	        pairList.add(new BasicNameValuePair("Mobile",phoneNo));
	        pairList.add(new BasicNameValuePair("Package",pkgNo));
	        pairList.add(new BasicNameValuePair("Sign",sign));
	        logger.info("云之讯订购请求参数："+pairList);
			HttpPost httpPost = new HttpPost(HOST_ORDER_PATH);
			CloseableHttpClient client = HttpClients.createDefault();
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(60000).build();//设置请求和传输超时时间
			httpPost.setConfig(requestConfig);
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
	
			
			if (200 == statusCode) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			
			}
			logger.info("云之讯订购订返回参数："+result+",该订单号为："+tradeNo);
		} catch (Exception e) {
		    logger.info("云之讯订购接口请求异常："+e.getMessage()+",该订单号为："+tradeNo);
		  
		}

	
		return result;
	}

	
	
	
	public String orderStatus(String cstmOrderNo) {
		
		String result = "";  
		try {
			String waitSign = "account="+YUNZX_ACCOUNT+"&outtradeno="+cstmOrderNo+"&key="+YUNZX_KEY;
			String sign = getMD5(waitSign);
			List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
			pairList.add(new BasicNameValuePair("Action", "getOrder"));
			pairList.add(new BasicNameValuePair("V", "1.1"));
			pairList.add(new BasicNameValuePair("Account", YUNZX_ACCOUNT));
			pairList.add(new BasicNameValuePair("OutTradeNo",cstmOrderNo));
			pairList.add(new BasicNameValuePair("Sign",sign));
			HttpPost httpPost = new HttpPost(HOST_ORDERSTATUS_PATH);
			CloseableHttpClient client = HttpClients.createDefault();
		
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();

			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {

			logger.info("云之查询接口请求异常："+e.getMessage()+",该订单号为："+cstmOrderNo);
	
		}
		return result;
	}

	
	
	

	/**
	 * md5加密 ，用于生成sign
	 * 
	 * @param message
	 * @return
	 */
	public static String getMD5(String message) {
		System.out.println("getMD5STR " + message);
		MessageDigest messageDigest = null;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(message.getBytes("UTF-8"));

			byte[] byteArray = messageDigest.digest();
			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		System.out.println("getMD5====>" + md5StrBuff.toString().toLowerCase());
		return md5StrBuff.toString().toLowerCase();// 字母小写
	}


	public static void main(String[] args){
		//new YunzxServiceImpl().order("YD10","13570313596","CX201707051621", "0");
		String result = new YunzxServiceImpl().orderStatus("CX201707051621");
		System.out.println(result);
	}


	
}
